<?php
/**
  * 验证数据格式
  *
  */

// 验证 Email
function check_email($email, $test_mx = false){
	$re = true;
	if(preg_match('/^[^0-9][a-zA-Z0-9_]+([.][a-zA-Z0-9_]+)*[@][a-zA-Z0-9_]+([.][a-zA-Z0-9_]+)*[.][a-zA-Z]{2,4}$/', $email)){
		if($test_mx){
			list($username, $domain) = split("@", $email);
			$re = getmxrr($domain, $mxrecords);
		}
	}else{
		$re = false;
	}

	return $re;
}

// 验证 IP
function check_ip($ipaddres) {
	$preg="/\A((([0-9]?[0-9])|(1[0-9]{2})|(2[0-4][0-9])|(25[0-5]))\.){3}(([0-9]?[0-9])|(1[0-9]{2})|(2[0-4][0-9])|(25[0-5]))\Z/";
	if(preg_match($preg,$ipaddres))
		return true;
	return false;
}

// 验证 URL
function check_url($url){
	return preg_match("/^http:\/\/[A-Za-z0-9]+\.[A-Za-z0-9]+[\/=\?%\-&_~`@[\]\':+!]*([^<>\"])*$/", $str);
}
